package com.erp.action;

import java.lang.reflect.ParameterizedType;

import com.opensymphony.xwork2.ModelDriven;

/**
 * 实现模型驱动的BaseAction
 * 
 * @param <T>
 */
@SuppressWarnings("serial")
public class ModelDrivenBaseAction<T> extends BaseAction implements
		ModelDriven<T> {

	protected T model;

	@SuppressWarnings("unchecked")
	@Override
	public T getModel() {
		try {
			// 得到model的类型信息
			Class<T> classType = (Class<T>) this.getClass();
			ParameterizedType pt = (ParameterizedType) classType
					.getGenericSuperclass();
			classType = (Class<T>) pt.getActualTypeArguments()[0];
			// 通过反射生成model实例
			model = (T) classType.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return model;
	}
}
